/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Database;

import Models.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Trang
 */
public class AuthenticationDAO {
     private Connection con;

    public AuthenticationDAO() {
        con = DBFactory.createConnection("dormitoryv2");
    }

    public boolean checkLogin(User memberaccount) {
        boolean result = false;

        String sqlst = "SELECT * FROM USER "
                + "WHERE Username = ? AND password = ?";
        try {
            PreparedStatement ps = con.prepareStatement(sqlst);
            ps.setString(1, memberaccount.getUserName());
            ps.setString(2, memberaccount.getPassword());

            ResultSet rs = ps.executeQuery();

            if (rs.next()) {
                result = true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(AuthenticationDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return result;
    }
    public User getUserFromUsername(String userName) {
        User result = new User();

        try {
            String sqlst = "SELECT * FROM user "
                    + "WHERE user.Username = ?";
            PreparedStatement ps = con.prepareStatement(sqlst);
            ps.setString(1, userName);

            ResultSet rs = ps.executeQuery();

            if (rs.next()) {
                Integer idUser = rs.getInt("userID");
                String password = rs.getString("password");
//                String email = rs.getString("email");
////                String loginName = rs.getString("loginName");
//                String password = rs.getString("password");
//                String firstName = rs.getString("firstName");
//                String lastName = rs.getString("lastName");
//                String address = rs.getString("address");
//                String role = rs.getString("role");
//                Date dob = (Date) rs.getDate("dob");
//                Date openTime = rs.getTimestamp("openTime");
//                Date closeTime = rs.getTimestamp("closeTime");

                result = new User(idUser,userName, password);
            }

            //role list
//            List<Role> roleList = new ArrayList<Role>();
//            
//            String sqlst2 = "SELECT role.* FROM staff, role, staffrole "
//                    + "WHERE staff.loginName = ? "
//                    + "AND staff.idStaff = staffrole.idStaff "
//                    + "AND staffrole.idRole = role.idRole";
//            PreparedStatement ps2 = con.prepareStatement(sqlst2);
//            ps2.setString(1, loginName);
//
//            ResultSet rs2 = ps2.executeQuery();
//            
//            while (rs2.next()) {
//                Integer idRole = rs2.getInt(1);
//                String name = rs2.getString(2);
//                
//                Role temp = new Role(idRole, name);
//                roleList.add(temp);
//            }
//            
//            result.setRoleList(roleList);
        } catch (SQLException ex) {
            Logger.getLogger(AuthenticationDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println(result.toString());
        return result;
    }
}
